﻿package com.app 
{
	import com.peachcastle.debug.Log;
	import com.peachcastle.debug.LocalDebug;
	import com.peachcastle.debug.FlashCookie;
	
	import flash.display.Sprite;
	
	import org.puremvc.as3.interfaces.IFacade;
	import org.puremvc.as3.patterns.facade.Facade;
	import org.puremvc.as3.patterns.observer.Notification;
	
	import com.app.controller.StartupCommand;
	import com.app.controller.HallCommand;
	import com.app.controller.RoomCommand;
	import com.app.controller.HttpCommand;
	import com.app.controller.NoticeCommand;
	
	import com.app.view.AppMediator;
	import com.app.view.manager.DialogManagerMediator;
	import com.app.view.manager.LayerManagerMediator;
	import com.app.view.component.RoomMediator;
	import com.app.view.component.HallMediator;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class AppFacade extends Facade implements IFacade
	{
		public static const NAME:String = 'AppFacade';
		public static const STARTUP:String  = NAME + '_Startup';
		public static const INIT_HALL:String = NAME + '_InitHall';
		public static const LOGIN_HALL:String = NAME + '_LoginHall';
		public static const ENTER_HALL:String = NAME + '_EnterHall';
		public static const ADD_NEW_ROOM:String = NAME + '_AddNewRoom';
		public static const UPDATE_ROOM:String = NAME + '_UpdateRoom';
		public static const HALL_START_GAME:String = NAME + '_HallStartGame';
		
		public static const READY_ENTER_ROOM:String = NAME + '_ReadyEnterRoom';
		public static const ENTER_ROOM:String = NAME + '_EnterRoom';
		public static const SIT_DOWN:String = NAME + '_SitDown';
		public static const STAND:String = NAME + '_Stand';
		public static const GO_HALL:String = NAME + '_GoHall';
		public static const START_GAME:String = NAME + '_StartGame';
		public static const ADD_BASE_POUR:String = NAME + '_AddBasePour';
		public static const CALL_POUR_START:String = NAME + '_CallPourStart';
		public static const CALL_POUR:String = NAME + '_CallPour';
		public static const START_PK:String = NAME + '_StartPk';
		public static const PK_RESULT:String = NAME + '_PkResult';
		public static const GAME_OVER:String = NAME + '_GameOver';
		
		public static const EXP_ADD:String = NAME + '_ExpAdd';
		
		public static const CHAT:String = NAME + '_Chat';
		public static const EXPRESSION:String = NAME + '_Expression';
		
		public static const HEARTBAET_USER_CLOSE:String = NAME + '_HeartbeatUserClose';
		
		public static const SERVER_NOTICE:String = NAME + '_ServerNotice';
		public static const CLIENT_NOTICE:String = NAME + '_ClientNotice';
		
		
		
		private var _stage:Array = null;
		
		public static function getInstance():AppFacade
		{
			if (instance == null)
				instance = new AppFacade();
				
			return instance as AppFacade;
		}
		
		override protected function initializeController():void
		{
			super.initializeController();
			
			registerCommand(AppFacade.STARTUP, StartupCommand);
			
			registerCommand(AppFacade.INIT_HALL, HallCommand);
			registerCommand(AppFacade.LOGIN_HALL, HallCommand);
			registerCommand(AppFacade.ENTER_HALL, HallCommand);
			registerCommand(AppFacade.ADD_NEW_ROOM, HallCommand);
			registerCommand(AppFacade.UPDATE_ROOM, HallCommand);
			registerCommand(DialogManagerMediator.CREATEROOM_SEND_INFO, HallCommand);
			
			registerCommand(AppFacade.HALL_START_GAME, HallCommand);
			registerCommand(HallMediator.START_GAME_REQUEST, HallCommand);
			
			registerCommand(AppMediator.ROOM_LOAD_OK, RoomCommand);
			
			registerCommand(RoomMediator.INIT_ROOM_REQUEST, RoomCommand);
			registerCommand(RoomMediator.SIT_DOWN_REQUEST, RoomCommand);
			registerCommand(RoomMediator.REAL_SIT_DOWN_REQUEST, RoomCommand);
			registerCommand(RoomMediator.STAND_REQUEST, RoomCommand);
			registerCommand(RoomMediator.GO_HALL_REQUEST, RoomCommand);
			registerCommand(RoomMediator.START_GAME_REQUEST, RoomCommand);
			registerCommand(RoomMediator.BASE_POUR_REQUEST, RoomCommand);
			registerCommand(RoomMediator.POUR_REQUEST, RoomCommand);
			registerCommand(RoomMediator.PK_RESULT_REQUEST, RoomCommand);
			
			registerCommand(RoomMediator.EXPRESSION_REQUEST, RoomCommand);
			registerCommand(RoomMediator.CHAT_REQUEST, RoomCommand);
			
			registerCommand(RoomMediator.SHOW_LAST_RESULT_REQUEST, RoomCommand);
			
			registerCommand(AppFacade.READY_ENTER_ROOM, RoomCommand);
			registerCommand(AppFacade.ENTER_ROOM, RoomCommand);
			registerCommand(AppFacade.SIT_DOWN, RoomCommand);
			registerCommand(AppFacade.STAND, RoomCommand);
			registerCommand(AppFacade.GO_HALL, RoomCommand);
			registerCommand(AppFacade.START_GAME, RoomCommand);
			registerCommand(AppFacade.ADD_BASE_POUR, RoomCommand);
			registerCommand(AppFacade.CALL_POUR_START, RoomCommand);
			registerCommand(AppFacade.CALL_POUR, RoomCommand);
			registerCommand(AppFacade.START_PK, RoomCommand);
			registerCommand(AppFacade.PK_RESULT, RoomCommand);
			registerCommand(AppFacade.GAME_OVER, RoomCommand);
			
			registerCommand(AppFacade.EXP_ADD, RoomCommand);
			
			registerCommand(AppFacade.CHAT, RoomCommand);
			registerCommand(AppFacade.EXPRESSION, RoomCommand);
			
			registerCommand(AppFacade.HEARTBAET_USER_CLOSE, RoomCommand);
			
			
			registerCommand(AppMediator.FEEDBACK_SEND_INFO_REQUEST, HttpCommand);
			registerCommand(AppMediator.FEEDBACK_SEND_AFFIX_REQUEST, HttpCommand);
			
			
			registerCommand(AppFacade.SERVER_NOTICE, NoticeCommand);
			registerCommand(AppFacade.CLIENT_NOTICE, NoticeCommand);
		}
		
		public function startup(stage:Array):void
		{
			Log.debug("startup");
			
			_stage = stage;
			
			if (_stage[1] != null) Log.console = false;

			switch(Log.console)
			{
				case true:
					registerMediator( new LayerManagerMediator() );
					sendNotification(LayerManagerMediator.SET_ROOT_LAYER, [_stage[0], 760, 700]);	//图层高度和宽度
					
					var localSprite:Sprite = LocalDebug.getInstance().showLocalDebug(onStartup);
					sendNotification(LayerManagerMediator.ADD_IN_LAYER, [LayerManagerMediator.PRINT_LAYER, localSprite]);
					
					break;
					
				case false:
					onStartup();
					
					break;
			}
		}
		
		private function onStartup():void
		{
			if (Log.console)
			{
				_stage[1] = new Object();
				_stage[1]["uid"] = LocalDebug.getInstance().uid;
				_stage[1]["mtkey"] = LocalDebug.getInstance().mtkey;
				_stage[1]["host"] = LocalDebug.getInstance().ip;
				_stage[1]["port"] = LocalDebug.getInstance().port;
			}
			
			sendNotification(AppFacade.STARTUP, _stage);
		}
		
		override public function sendNotification(notificationName:String, body:Object = null, type:String = null):void
		{
			notifyObservers(new Notification(notificationName, body, type));
		}
	}

}